ServerResponseHelper.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 System.IO;
  10. using System.Text;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server.SMB1
  14. {
  15. public partial class ServerResponseHelper
  16. {
  17. internal static SMB1Command GetCloseResponse(SMB1Header header, CloseRequest request, ISMBShare share, SMB1ConnectionState state)
  18. {
  19. SMB1Session session = state.GetSession(header.UID);
  20. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  21. if (openFile == null)
  22. {
  23. header.Status = NTStatus.STATUS_SMB_BAD_FID;
  24. return new ErrorResponse(CommandName.SMB_COM_CLOSE);
  25. }
  26. state.LogToServer(Severity.Verbose, "Close: Closing file '{0}'", openFile.Path);
  27. session.RemoveOpenFile(request.FID);
  28. if (openFile.DeleteOnClose && share is FileSystemShare)
  29. {
  30. try
  31. {
  32. ((FileSystemShare)share).FileSystem.Delete(openFile.Path);
  33. }
  34. catch
  35. {
  36. state.LogToServer(Severity.Debug, "Close: Cannot delete '{0}'", openFile.Path);
  37. }
  38. }
  39. CloseResponse response = new CloseResponse();
  40. return response;
  41. }
  42. internal static SMB1Command GetFindClose2Request(SMB1Header header, FindClose2Request request, SMB1ConnectionState state)
  43. {
  44. SMB1Session session = state.GetSession(header.UID);
  45. session.ReleaseSearchHandle(request.SearchHandle);
  46. return new FindClose2Response();
  47. }
  48. internal static EchoResponse GetEchoResponse(EchoRequest request, List<SMB1Command> sendQueue)
  49. {
  50. EchoResponse response = new EchoResponse();
  51. response.SequenceNumber = 0;
  52. response.SMBData = request.SMBData;
  53. for (int index = 1; index < request.EchoCount; index++)
  54. {
  55. EchoResponse echo = new EchoResponse();
  56. echo.SequenceNumber = (ushort)index;
  57. echo.SMBData = request.SMBData;
  58. sendQueue.Add(echo);
  59. }
  60. return response;
  61. }
  62. }
  63. }