SMBServer.SMB2.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. m_securityProvider.DeleteSecurityContext(ref session.SecurityContext.AuthenticationContext);
  127. state.RemoveSession(command.Header.SessionID);
  128. return new LogoffResponse();
  129. }
  130. else
  131. {
  132. ISMBShare share = session.GetConnectedTree(command.Header.TreeID);
  133. if (share == null)
  134. {
  135. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NETWORK_NAME_DELETED);
  136. }
  137. if (command is TreeDisconnectRequest)
  138. {
  139. session.RemoveConnectedTree(command.Header.TreeID);
  140. return new TreeDisconnectResponse();
  141. }
  142. else if (command is CreateRequest)
  143. {
  144. return CreateHelper.GetCreateResponse((CreateRequest)command, share, state);
  145. }
  146. else if (command is QueryInfoRequest)
  147. {
  148. return QueryInfoHelper.GetQueryInfoResponse((QueryInfoRequest)command, share, state);
  149. }
  150. else if (command is SetInfoRequest)
  151. {
  152. return SetInfoHelper.GetSetInfoResponse((SetInfoRequest)command, share, state);
  153. }
  154. else if (command is QueryDirectoryRequest)
  155. {
  156. return QueryDirectoryHelper.GetQueryDirectoryResponse((QueryDirectoryRequest)command, share, state);
  157. }
  158. else if (command is ReadRequest)
  159. {
  160. return ReadWriteResponseHelper.GetReadResponse((ReadRequest)command, share, state);
  161. }
  162. else if (command is WriteRequest)
  163. {
  164. return ReadWriteResponseHelper.GetWriteResponse((WriteRequest)command, share, state);
  165. }
  166. else if (command is FlushRequest)
  167. {
  168. FlushRequest request = (FlushRequest)command;
  169. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  170. if (openFile == null)
  171. {
  172. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  173. }
  174. NTStatus status = share.FileStore.FlushFileBuffers(openFile.Handle);
  175. if (status != NTStatus.STATUS_SUCCESS)
  176. {
  177. return new ErrorResponse(request.CommandName, status);
  178. }
  179. return new FlushResponse();
  180. }
  181. else if (command is CloseRequest)
  182. {
  183. return CloseHelper.GetCloseResponse((CloseRequest)command, share, state);
  184. }
  185. else if (command is IOCtlRequest)
  186. {
  187. return IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, state);
  188. }
  189. else if (command is ChangeNotifyRequest)
  190. {
  191. // [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
  192. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  193. }
  194. }
  195. }
  196. return new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  197. }
  198. public static void TrySendResponse(ConnectionState state, SMB2Command response)
  199. {
  200. SessionMessagePacket packet = new SessionMessagePacket();
  201. packet.Trailer = response.GetBytes();
  202. TrySendPacket(state, packet);
  203. state.LogToServer(Severity.Verbose, "SMB2 response sent: {0}, Packet length: {1}", response.CommandName.ToString(), packet.Length);
  204. }
  205. public static void TrySendResponseChain(ConnectionState state, List<SMB2Command> responseChain)
  206. {
  207. SessionMessagePacket packet = new SessionMessagePacket();
  208. packet.Trailer = SMB2Command.GetCommandChainBytes(responseChain);
  209. TrySendPacket(state, packet);
  210. 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);
  211. }
  212. private static void UpdateSMB2Header(SMB2Command response, SMB2Command request)
  213. {
  214. response.Header.MessageID = request.Header.MessageID;
  215. response.Header.CreditCharge = request.Header.CreditCharge;
  216. response.Header.Credits = Math.Max((ushort)1, request.Header.Credits);
  217. response.Header.IsRelatedOperations = request.Header.IsRelatedOperations;
  218. response.Header.Reserved = request.Header.Reserved;
  219. if (response.Header.SessionID == 0)
  220. {
  221. response.Header.SessionID = request.Header.SessionID;
  222. }
  223. if (response.Header.TreeID == 0)
  224. {
  225. response.Header.TreeID = request.Header.TreeID;
  226. }
  227. }
  228. private static void SetRequestFileID(SMB2Command command, FileID fileID)
  229. {
  230. if (command is ChangeNotifyRequest)
  231. {
  232. ((ChangeNotifyRequest)command).FileId = fileID;
  233. }
  234. else if (command is CloseRequest)
  235. {
  236. ((CloseRequest)command).FileId = fileID;
  237. }
  238. else if (command is FlushRequest)
  239. {
  240. ((FlushRequest)command).FileId = fileID;
  241. }
  242. else if (command is IOCtlRequest)
  243. {
  244. ((IOCtlRequest)command).FileId = fileID;
  245. }
  246. else if (command is LockRequest)
  247. {
  248. ((LockRequest)command).FileId = fileID;
  249. }
  250. else if (command is QueryDirectoryRequest)
  251. {
  252. ((QueryDirectoryRequest)command).FileId = fileID;
  253. }
  254. else if (command is QueryInfoRequest)
  255. {
  256. ((QueryInfoRequest)command).FileId = fileID;
  257. }
  258. else if (command is ReadRequest)
  259. {
  260. ((ReadRequest)command).FileId = fileID;
  261. }
  262. else if (command is SetInfoRequest)
  263. {
  264. ((SetInfoRequest)command).FileId = fileID;
  265. }
  266. else if (command is WriteRequest)
  267. {
  268. ((WriteRequest)command).FileId = fileID;
  269. }
  270. }
  271. private static FileID? GetResponseFileID(SMB2Command command)
  272. {
  273. if (command is CreateResponse)
  274. {
  275. return ((CreateResponse)command).FileId;
  276. }
  277. else if (command is IOCtlResponse)
  278. {
  279. return ((IOCtlResponse)command).FileId;
  280. }
  281. return null;
  282. }
  283. }
  284. }