TransactionSubcommandHelper.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Text;
  10. using SMBLibrary.RPC;
  11. using SMBLibrary.SMB1;
  12. using SMBLibrary.Services;
  13. using Utilities;
  14. namespace SMBLibrary.Server.SMB1
  15. {
  16. internal class TransactionSubcommandHelper
  17. {
  18. internal static TransactionTransactNamedPipeResponse GetSubcommandResponse(SMB1Header header, uint maxDataCount, TransactionTransactNamedPipeRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  19. {
  20. SMB1Session session = state.GetSession(header.UID);
  21. OpenFileObject openFile = session.GetOpenFileObject(subcommand.FID);
  22. if (openFile == null)
  23. {
  24. state.LogToServer(Severity.Verbose, "TransactNamedPipe failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, subcommand.FID);
  25. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  26. return null;
  27. }
  28. int maxOutputLength = (int)maxDataCount;
  29. byte[] output;
  30. header.Status = share.FileStore.DeviceIOControl(openFile.Handle, (uint)IoControlCode.FSCTL_PIPE_TRANSCEIVE, subcommand.WriteData, out output, maxOutputLength);
  31. if (header.Status != NTStatus.STATUS_SUCCESS && header.Status != NTStatus.STATUS_BUFFER_OVERFLOW)
  32. {
  33. state.LogToServer(Severity.Verbose, "TransactNamedPipe failed. NTStatus: {0}.", header.Status);
  34. return null;
  35. }
  36. TransactionTransactNamedPipeResponse response = new TransactionTransactNamedPipeResponse();
  37. response.ReadData = output;
  38. return response;
  39. }
  40. internal static void ProcessSubcommand(SMB1Header header, uint timeout, string name, TransactionWaitNamedPipeRequest subcommand, ISMBShare share, SMB1ConnectionState state)
  41. {
  42. if (!name.StartsWith(@"\PIPE\", StringComparison.OrdinalIgnoreCase))
  43. {
  44. // [MS-CIFS] The name field MUST be set to the name of the pipe being waited for, in the format \PIPE\<pipename>
  45. state.LogToServer(Severity.Verbose, "TransactWaitNamedPipe failed. Invalid pipe name: {0}.", name);
  46. header.Status = NTStatus.STATUS_INVALID_SMB;
  47. }
  48. string pipeName = name.Substring(6);
  49. PipeWaitRequest pipeWaitRequest = new PipeWaitRequest();
  50. pipeWaitRequest.Timeout = timeout;
  51. pipeWaitRequest.TimeSpecified = true;
  52. pipeWaitRequest.Name = pipeName;
  53. byte[] input = pipeWaitRequest.GetBytes();
  54. byte[] output;
  55. header.Status = share.FileStore.DeviceIOControl(null, (uint)IoControlCode.FSCTL_PIPE_WAIT, input, out output, 0);
  56. if (header.Status != NTStatus.STATUS_SUCCESS)
  57. {
  58. state.LogToServer(Severity.Verbose, "TransactWaitNamedPipe failed. Pipe name: {0}. NTStatus: {1}.", pipeName, header.Status);
  59. }
  60. else
  61. {
  62. state.LogToServer(Severity.Verbose, "TransactWaitNamedPipe succeeded. Pipe name: {0}.", pipeName);
  63. }
  64. }
  65. }
  66. }