SMBServer.SMB2.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. public void ProcessSMB2RequestChain(List<SMB2Command> requestChain, ref ConnectionState state)
  38. {
  39. List<SMB2Command> responseChain = new List<SMB2Command>();
  40. FileID? fileID = null;
  41. foreach (SMB2Command request in requestChain)
  42. {
  43. if (request.Header.IsRelatedOperations && fileID.HasValue)
  44. {
  45. SetRequestFileID(request, fileID.Value);
  46. }
  47. SMB2Command response = ProcessSMB2Command(request, ref state);
  48. if (response != null)
  49. {
  50. UpdateSMB2Header(response, request);
  51. responseChain.Add(response);
  52. if (!request.Header.IsRelatedOperations)
  53. {
  54. fileID = GetResponseFileID(response);
  55. }
  56. }
  57. }
  58. if (responseChain.Count > 0)
  59. {
  60. TrySendResponseChain(state, responseChain);
  61. }
  62. }
  63. /// <summary>
  64. /// May return null
  65. /// </summary>
  66. public SMB2Command ProcessSMB2Command(SMB2Command command, ref ConnectionState state)
  67. {
  68. if (state.ServerDialect == SMBDialect.NotSet)
  69. {
  70. if (command is NegotiateRequest)
  71. {
  72. NegotiateRequest request = (NegotiateRequest)command;
  73. SMB2Command response = NegotiateHelper.GetNegotiateResponse(request, m_securityProvider, state, m_serverGuid);
  74. if (state.ServerDialect != SMBDialect.NotSet)
  75. {
  76. state = new SMB2ConnectionState(state, AllocatePersistentFileID);
  77. }
  78. return response;
  79. }
  80. else
  81. {
  82. // [MS-SMB2] If the request being received is not an SMB2 NEGOTIATE Request [..]
  83. // and Connection.NegotiateDialect is 0xFFFF or 0x02FF, the server MUST
  84. // disconnect the connection.
  85. state.LogToServer(Severity.Debug, "Invalid Connection State for command {0}", command.CommandName.ToString());
  86. state.ClientSocket.Close();
  87. return null;
  88. }
  89. }
  90. else if (command is NegotiateRequest)
  91. {
  92. // [MS-SMB2] If Connection.NegotiateDialect is 0x0202, 0x0210, 0x0300, 0x0302, or 0x0311,
  93. // the server MUST disconnect the connection.
  94. state.LogToServer(Severity.Debug, "Rejecting NegotiateRequest. NegotiateDialect is already set");
  95. state.ClientSocket.Close();
  96. return null;
  97. }
  98. else
  99. {
  100. return ProcessSMB2Command(command, (SMB2ConnectionState)state);
  101. }
  102. }
  103. public SMB2Command ProcessSMB2Command(SMB2Command command, SMB2ConnectionState state)
  104. {
  105. if (command is SessionSetupRequest)
  106. {
  107. return SessionSetupHelper.GetSessionSetupResponse((SessionSetupRequest)command, m_securityProvider, state);
  108. }
  109. else if (command is EchoRequest)
  110. {
  111. return new EchoResponse();
  112. }
  113. else
  114. {
  115. SMB2Session session = state.GetSession(command.Header.SessionID);
  116. if (session == null)
  117. {
  118. return new ErrorResponse(command.CommandName, NTStatus.STATUS_USER_SESSION_DELETED);
  119. }
  120. if (command is TreeConnectRequest)
  121. {
  122. return TreeConnectHelper.GetTreeConnectResponse((TreeConnectRequest)command, state, m_services, m_shares);
  123. }
  124. else if (command is LogoffRequest)
  125. {
  126. state.RemoveSession(command.Header.SessionID);
  127. return new LogoffResponse();
  128. }
  129. else
  130. {
  131. ISMBShare share = session.GetConnectedTree(command.Header.TreeID);
  132. if (share == null)
  133. {
  134. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NETWORK_NAME_DELETED);
  135. }
  136. if (command is TreeDisconnectRequest)
  137. {
  138. session.RemoveConnectedTree(command.Header.TreeID);
  139. return new TreeDisconnectResponse();
  140. }
  141. else if (command is CreateRequest)
  142. {
  143. return CreateHelper.GetCreateResponse((CreateRequest)command, share, state);
  144. }
  145. else if (command is QueryInfoRequest)
  146. {
  147. return QueryInfoHelper.GetQueryInfoResponse((QueryInfoRequest)command, share, state);
  148. }
  149. else if (command is SetInfoRequest)
  150. {
  151. return SetInfoHelper.GetSetInfoResponse((SetInfoRequest)command, share, state);
  152. }
  153. else if (command is QueryDirectoryRequest)
  154. {
  155. return QueryDirectoryHelper.GetQueryDirectoryResponse((QueryDirectoryRequest)command, share, state);
  156. }
  157. else if (command is ReadRequest)
  158. {
  159. return ReadWriteResponseHelper.GetReadResponse((ReadRequest)command, share, state);
  160. }
  161. else if (command is WriteRequest)
  162. {
  163. return ReadWriteResponseHelper.GetWriteResponse((WriteRequest)command, share, state);
  164. }
  165. else if (command is FlushRequest)
  166. {
  167. FlushRequest request = (FlushRequest)command;
  168. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  169. if (openFile == null)
  170. {
  171. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  172. }
  173. NTStatus status = share.FileStore.FlushFileBuffers(openFile.Handle);
  174. if (status != NTStatus.STATUS_SUCCESS)
  175. {
  176. return new ErrorResponse(request.CommandName, status);
  177. }
  178. return new FlushResponse();
  179. }
  180. else if (command is CloseRequest)
  181. {
  182. return CloseHelper.GetCloseResponse((CloseRequest)command, share, state);
  183. }
  184. else if (command is IOCtlRequest)
  185. {
  186. return IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, state);
  187. }
  188. else if (command is ChangeNotifyRequest)
  189. {
  190. // [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
  191. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  192. }
  193. }
  194. }
  195. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  196. }
  197. public static void TrySendResponse(ConnectionState state, SMB2Command response)
  198. {
  199. SessionMessagePacket packet = new SessionMessagePacket();
  200. packet.Trailer = response.GetBytes();
  201. TrySendPacket(state, packet);
  202. state.LogToServer(Severity.Verbose, "SMB2 response sent: {0}, Packet length: {1}", response.CommandName.ToString(), packet.Length);
  203. }
  204. public static void TrySendResponseChain(ConnectionState state, List<SMB2Command> responseChain)
  205. {
  206. SessionMessagePacket packet = new SessionMessagePacket();
  207. packet.Trailer = SMB2Command.GetCommandChainBytes(responseChain);
  208. TrySendPacket(state, packet);
  209. 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);
  210. }
  211. private static void UpdateSMB2Header(SMB2Command response, SMB2Command request)
  212. {
  213. response.Header.MessageID = request.Header.MessageID;
  214. response.Header.CreditCharge = request.Header.CreditCharge;
  215. response.Header.Credits = Math.Max((ushort)1, request.Header.Credits);
  216. response.Header.IsRelatedOperations = request.Header.IsRelatedOperations;
  217. response.Header.Reserved = request.Header.Reserved;
  218. if (response.Header.SessionID == 0)
  219. {
  220. response.Header.SessionID = request.Header.SessionID;
  221. }
  222. if (response.Header.TreeID == 0)
  223. {
  224. response.Header.TreeID = request.Header.TreeID;
  225. }
  226. }
  227. private static void SetRequestFileID(SMB2Command command, FileID fileID)
  228. {
  229. if (command is ChangeNotifyRequest)
  230. {
  231. ((ChangeNotifyRequest)command).FileId = fileID;
  232. }
  233. else if (command is CloseRequest)
  234. {
  235. ((CloseRequest)command).FileId = fileID;
  236. }
  237. else if (command is FlushRequest)
  238. {
  239. ((FlushRequest)command).FileId = fileID;
  240. }
  241. else if (command is IOCtlRequest)
  242. {
  243. ((IOCtlRequest)command).FileId = fileID;
  244. }
  245. else if (command is LockRequest)
  246. {
  247. ((LockRequest)command).FileId = fileID;
  248. }
  249. else if (command is QueryDirectoryRequest)
  250. {
  251. ((QueryDirectoryRequest)command).FileId = fileID;
  252. }
  253. else if (command is QueryInfoRequest)
  254. {
  255. ((QueryInfoRequest)command).FileId = fileID;
  256. }
  257. else if (command is ReadRequest)
  258. {
  259. ((ReadRequest)command).FileId = fileID;
  260. }
  261. else if (command is SetInfoRequest)
  262. {
  263. ((SetInfoRequest)command).FileId = fileID;
  264. }
  265. else if (command is WriteRequest)
  266. {
  267. ((WriteRequest)command).FileId = fileID;
  268. }
  269. }
  270. private static FileID? GetResponseFileID(SMB2Command command)
  271. {
  272. if (command is CreateResponse)
  273. {
  274. return ((CreateResponse)command).FileId;
  275. }
  276. else if (command is IOCtlResponse)
  277. {
  278. return ((IOCtlResponse)command).FileId;
  279. }
  280. return null;
  281. }
  282. }
  283. }