IOCtlHelper.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. internal static SMB2Command GetIOCtlResponse(IOCtlRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. if (request.CtlCode == (uint)IoControlCode.FSCTL_DFS_GET_REFERRALS ||
  20. request.CtlCode == (uint)IoControlCode.FSCTL_DFS_GET_REFERRALS_EX)
  21. {
  22. // [MS-SMB2] 3.3.5.15.2 Handling a DFS Referral Information Request
  23. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FS_DRIVER_REQUIRED);
  24. }
  25. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  26. if (openFile == null)
  27. {
  28. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  29. }
  30. int maxOutputLength = (int)request.MaxOutputResponse;
  31. byte[] output;
  32. NTStatus status = share.FileStore.DeviceIOControl(openFile.Handle, request.CtlCode, request.Input, out output, maxOutputLength);
  33. if (status != NTStatus.STATUS_SUCCESS)
  34. {
  35. return new ErrorResponse(request.CommandName, status);
  36. }
  37. IOCtlResponse response = new IOCtlResponse();
  38. response.CtlCode = request.CtlCode;
  39. response.Output = output;
  40. return response;
  41. }
  42. }
  43. }