IOCtlHelper.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 System.IO;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. public class IOCtlHelper
  15. {
  16. private const uint FSCTL_DFS_GET_REFERRALS = 0x00060194;
  17. private const uint FSCTL_DFS_GET_REFERRALS_EX = 0x000601B0;
  18. private const uint FSCTL_PIPE_TRANSCEIVE = 0x0011C017;
  19. internal static SMB2Command GetIOCtlResponse(IOCtlRequest request, ISMBShare share, SMB2ConnectionState state)
  20. {
  21. SMB2Session session = state.GetSession(request.Header.SessionID);
  22. if (request.CtlCode == FSCTL_DFS_GET_REFERRALS || request.CtlCode == FSCTL_DFS_GET_REFERRALS_EX)
  23. {
  24. // [MS-SMB2] 3.3.5.15.2 Handling a DFS Referral Information Request
  25. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FS_DRIVER_REQUIRED);
  26. }
  27. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  28. if (openFile == null)
  29. {
  30. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  31. }
  32. if (share is NamedPipeShare)
  33. {
  34. if (request.CtlCode == FSCTL_PIPE_TRANSCEIVE)
  35. {
  36. IOCtlResponse response = new IOCtlResponse();
  37. response.CtlCode = request.CtlCode;
  38. openFile.Stream.Write(request.Input, 0, request.Input.Length);
  39. response.Output = ByteReader.ReadAllBytes(openFile.Stream);
  40. return response;
  41. }
  42. }
  43. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  44. }
  45. }
  46. }