SMBServer.SMB1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright (C) 2014-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.SMB1;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public partial class SMBServer
  16. {
  17. public void ProcessSMB1Message(SMB1Message message, SMB1ConnectionState state)
  18. {
  19. SMB1Message reply = new SMB1Message();
  20. PrepareResponseHeader(reply, message);
  21. List<SMB1Command> sendQueue = new List<SMB1Command>();
  22. foreach (SMB1Command command in message.Commands)
  23. {
  24. SMB1Command response = ProcessSMB1Command(reply.Header, command, state, sendQueue);
  25. if (response != null)
  26. {
  27. reply.Commands.Add(response);
  28. }
  29. if (reply.Header.Status != NTStatus.STATUS_SUCCESS)
  30. {
  31. break;
  32. }
  33. }
  34. if (reply.Commands.Count > 0)
  35. {
  36. TrySendMessage(state, reply);
  37. foreach (SMB1Command command in sendQueue)
  38. {
  39. SMB1Message secondaryReply = new SMB1Message();
  40. secondaryReply.Header = reply.Header;
  41. secondaryReply.Commands.Add(command);
  42. TrySendMessage(state, secondaryReply);
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// May return null
  48. /// </summary>
  49. public SMB1Command ProcessSMB1Command(SMB1Header header, SMB1Command command, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  50. {
  51. if (command is NegotiateRequest)
  52. {
  53. NegotiateRequest request = (NegotiateRequest)command;
  54. if (request.Dialects.Contains(SMBServer.NTLanManagerDialect))
  55. {
  56. if (EnableExtendedSecurity && header.ExtendedSecurityFlag)
  57. {
  58. return NegotiateHelper.GetNegotiateResponseExtended(request, m_serverGuid);
  59. }
  60. else
  61. {
  62. return NegotiateHelper.GetNegotiateResponse(header, request, m_users);
  63. }
  64. }
  65. else
  66. {
  67. return new NegotiateResponseNotSupported();
  68. }
  69. }
  70. else if (command is SessionSetupAndXRequest)
  71. {
  72. SessionSetupAndXRequest request = (SessionSetupAndXRequest)command;
  73. state.MaxBufferSize = request.MaxBufferSize;
  74. return SessionSetupHelper.GetSessionSetupResponse(header, request, m_users, state);
  75. }
  76. else if (command is SessionSetupAndXRequestExtended)
  77. {
  78. SessionSetupAndXRequestExtended request = (SessionSetupAndXRequestExtended)command;
  79. state.MaxBufferSize = request.MaxBufferSize;
  80. return SessionSetupHelper.GetSessionSetupResponseExtended(header, request, m_users, state);
  81. }
  82. else if (command is EchoRequest)
  83. {
  84. return ServerResponseHelper.GetEchoResponse((EchoRequest)command, sendQueue);
  85. }
  86. else if (state.IsAuthenticated(header.UID))
  87. {
  88. if (command is TreeConnectAndXRequest)
  89. {
  90. TreeConnectAndXRequest request = (TreeConnectAndXRequest)command;
  91. return TreeConnectHelper.GetTreeConnectResponse(header, request, state, m_services, m_shares);
  92. }
  93. else if (command is LogoffAndXRequest)
  94. {
  95. // FIXME: Remove connected trees that the user has connected to
  96. state.RemoveConnectedUser(header.UID);
  97. return new LogoffAndXResponse();
  98. }
  99. else if (state.IsTreeConnected(header.TID))
  100. {
  101. ISMBShare share = state.GetConnectedTree(header.TID);
  102. if (command is CreateDirectoryRequest)
  103. {
  104. if (!(share is FileSystemShare))
  105. {
  106. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  107. return new ErrorResponse(command.CommandName);
  108. }
  109. CreateDirectoryRequest request = (CreateDirectoryRequest)command;
  110. return FileSystemResponseHelper.GetCreateDirectoryResponse(header, request, (FileSystemShare)share, state);
  111. }
  112. else if (command is DeleteDirectoryRequest)
  113. {
  114. if (!(share is FileSystemShare))
  115. {
  116. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  117. return new ErrorResponse(command.CommandName);
  118. }
  119. DeleteDirectoryRequest request = (DeleteDirectoryRequest)command;
  120. return FileSystemResponseHelper.GetDeleteDirectoryResponse(header, request, (FileSystemShare)share, state);
  121. }
  122. else if (command is CloseRequest)
  123. {
  124. CloseRequest request = (CloseRequest)command;
  125. return ServerResponseHelper.GetCloseResponse(header, request, share, state);
  126. }
  127. else if (command is FlushRequest)
  128. {
  129. return new FlushResponse();
  130. }
  131. else if (command is DeleteRequest)
  132. {
  133. if (!(share is FileSystemShare))
  134. {
  135. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  136. return new ErrorResponse(command.CommandName);
  137. }
  138. DeleteRequest request = (DeleteRequest)command;
  139. return FileSystemResponseHelper.GetDeleteResponse(header, request, (FileSystemShare)share, state);
  140. }
  141. else if (command is RenameRequest)
  142. {
  143. if (!(share is FileSystemShare))
  144. {
  145. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  146. return new ErrorResponse(command.CommandName);
  147. }
  148. RenameRequest request = (RenameRequest)command;
  149. return FileSystemResponseHelper.GetRenameResponse(header, request, (FileSystemShare)share, state);
  150. }
  151. else if (command is QueryInformationRequest)
  152. {
  153. if (!(share is FileSystemShare))
  154. {
  155. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  156. return new ErrorResponse(command.CommandName);
  157. }
  158. QueryInformationRequest request = (QueryInformationRequest)command;
  159. return FileSystemResponseHelper.GetQueryInformationResponse(header, request, (FileSystemShare)share);
  160. }
  161. else if (command is SetInformationRequest)
  162. {
  163. if (!(share is FileSystemShare))
  164. {
  165. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  166. return new ErrorResponse(command.CommandName);
  167. }
  168. SetInformationRequest request = (SetInformationRequest)command;
  169. return FileSystemResponseHelper.GetSetInformationResponse(header, request, (FileSystemShare)share, state);
  170. }
  171. else if (command is ReadRequest)
  172. {
  173. ReadRequest request = (ReadRequest)command;
  174. return ReadWriteResponseHelper.GetReadResponse(header, request, share, state);
  175. }
  176. else if (command is WriteRequest)
  177. {
  178. string userName = state.GetConnectedUserName(header.UID);
  179. if (share is FileSystemShare && !((FileSystemShare)share).HasWriteAccess(userName))
  180. {
  181. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  182. return new ErrorResponse(command.CommandName);
  183. }
  184. WriteRequest request = (WriteRequest)command;
  185. return ReadWriteResponseHelper.GetWriteResponse(header, request, share, state);
  186. }
  187. else if (command is CheckDirectoryRequest)
  188. {
  189. if (!(share is FileSystemShare))
  190. {
  191. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  192. return new ErrorResponse(command.CommandName);
  193. }
  194. CheckDirectoryRequest request = (CheckDirectoryRequest)command;
  195. return FileSystemResponseHelper.GetCheckDirectoryResponse(header, request, (FileSystemShare)share);
  196. }
  197. else if (command is WriteRawRequest)
  198. {
  199. // [MS-CIFS] 3.3.5.26 - Receiving an SMB_COM_WRITE_RAW Request:
  200. // the server MUST verify that the Server.Capabilities include CAP_RAW_MODE,
  201. // If an error is detected [..] the Write Raw operation MUST fail and
  202. // the server MUST return a Final Server Response [..] with the Count field set to zero.
  203. return new WriteRawFinalResponse();
  204. }
  205. else if (command is SetInformation2Request)
  206. {
  207. if (!(share is FileSystemShare))
  208. {
  209. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  210. return new ErrorResponse(command.CommandName);
  211. }
  212. SetInformation2Request request = (SetInformation2Request)command;
  213. return FileSystemResponseHelper.GetSetInformation2Response(header, request, (FileSystemShare)share, state);
  214. }
  215. else if (command is LockingAndXRequest)
  216. {
  217. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  218. return new ErrorResponse(CommandName.SMB_COM_LOCKING_ANDX);
  219. }
  220. else if (command is OpenAndXRequest)
  221. {
  222. OpenAndXRequest request = (OpenAndXRequest)command;
  223. return OpenAndXHelper.GetOpenAndXResponse(header, request, share, state);
  224. }
  225. else if (command is ReadAndXRequest)
  226. {
  227. ReadAndXRequest request = (ReadAndXRequest)command;
  228. return ReadWriteResponseHelper.GetReadResponse(header, request, share, state);
  229. }
  230. else if (command is WriteAndXRequest)
  231. {
  232. string userName = state.GetConnectedUserName(header.UID);
  233. if (share is FileSystemShare && !((FileSystemShare)share).HasWriteAccess(userName))
  234. {
  235. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  236. return new ErrorResponse(command.CommandName);
  237. }
  238. WriteAndXRequest request = (WriteAndXRequest)command;
  239. return ReadWriteResponseHelper.GetWriteResponse(header, request, share, state);
  240. }
  241. else if (command is FindClose2Request)
  242. {
  243. return ServerResponseHelper.GetFindClose2Request(header, (FindClose2Request)command, state);
  244. }
  245. else if (command is TreeDisconnectRequest)
  246. {
  247. TreeDisconnectRequest request = (TreeDisconnectRequest)command;
  248. return TreeConnectHelper.GetTreeDisconnectResponse(header, request, state);
  249. }
  250. else if (command is TransactionRequest) // Both TransactionRequest and Transaction2Request
  251. {
  252. TransactionRequest request = (TransactionRequest)command;
  253. try
  254. {
  255. return TransactionHelper.GetTransactionResponse(header, request, share, state, sendQueue);
  256. }
  257. catch (UnsupportedInformationLevelException)
  258. {
  259. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  260. return new ErrorResponse(command.CommandName);
  261. }
  262. }
  263. else if (command is TransactionSecondaryRequest) // Both TransactionSecondaryRequest and Transaction2SecondaryRequest
  264. {
  265. TransactionSecondaryRequest request = (TransactionSecondaryRequest)command;
  266. try
  267. {
  268. return TransactionHelper.GetTransactionResponse(header, request, share, state, sendQueue);
  269. }
  270. catch (UnsupportedInformationLevelException)
  271. {
  272. header.Status = NTStatus.STATUS_INVALID_PARAMETER;
  273. return new ErrorResponse(command.CommandName);
  274. }
  275. }
  276. else if (command is NTTransactRequest)
  277. {
  278. NTTransactRequest request = (NTTransactRequest)command;
  279. return NTTransactHelper.GetNTTransactResponse(header, request, share, state, sendQueue);
  280. }
  281. else if (command is NTTransactSecondaryRequest)
  282. {
  283. NTTransactSecondaryRequest request = (NTTransactSecondaryRequest)command;
  284. return NTTransactHelper.GetNTTransactResponse(header, request, share, state, sendQueue);
  285. }
  286. else if (command is NTCreateAndXRequest)
  287. {
  288. NTCreateAndXRequest request = (NTCreateAndXRequest)command;
  289. return NTCreateHelper.GetNTCreateResponse(header, request, share, state);
  290. }
  291. }
  292. else
  293. {
  294. header.Status = NTStatus.STATUS_SMB_BAD_TID;
  295. return new ErrorResponse(command.CommandName);
  296. }
  297. }
  298. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  299. return new ErrorResponse(command.CommandName);
  300. }
  301. public static void TrySendMessage(SMB1ConnectionState state, SMB1Message response)
  302. {
  303. SessionMessagePacket packet = new SessionMessagePacket();
  304. packet.Trailer = response.GetBytes();
  305. TrySendPacket(state, packet);
  306. state.LogToServer(Severity.Verbose, "Response sent: {0} Commands, First Command: {1}, Packet length: {2}", response.Commands.Count, response.Commands[0].CommandName.ToString(), packet.Length);
  307. }
  308. private static void PrepareResponseHeader(SMB1Message response, SMB1Message request)
  309. {
  310. response.Header.Status = NTStatus.STATUS_SUCCESS;
  311. response.Header.Flags = HeaderFlags.CaseInsensitive | HeaderFlags.CanonicalizedPaths | HeaderFlags.Reply;
  312. response.Header.Flags2 = HeaderFlags2.NTStatusCode;
  313. if ((request.Header.Flags2 & HeaderFlags2.LongNamesAllowed) > 0)
  314. {
  315. response.Header.Flags2 |= HeaderFlags2.LongNamesAllowed | HeaderFlags2.LongNameUsed;
  316. }
  317. if ((request.Header.Flags2 & HeaderFlags2.ExtendedAttributes) > 0)
  318. {
  319. response.Header.Flags2 |= HeaderFlags2.ExtendedAttributes;
  320. }
  321. if ((request.Header.Flags2 & HeaderFlags2.ExtendedSecurity) > 0)
  322. {
  323. response.Header.Flags2 |= HeaderFlags2.ExtendedSecurity;
  324. }
  325. if ((request.Header.Flags2 & HeaderFlags2.Unicode) > 0)
  326. {
  327. response.Header.Flags2 |= HeaderFlags2.Unicode;
  328. }
  329. response.Header.MID = request.Header.MID;
  330. response.Header.PID = request.Header.PID;
  331. response.Header.UID = request.Header.UID;
  332. response.Header.TID = request.Header.TID;
  333. }
  334. }
  335. }