NamedPipeStore.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.RPC;
  11. using SMBLibrary.Services;
  12. using Utilities;
  13. namespace SMBLibrary
  14. {
  15. public class NamedPipeStore : INTFileStore
  16. {
  17. private List<RemoteService> m_services;
  18. public NamedPipeStore(List<RemoteService> services)
  19. {
  20. m_services = services;
  21. }
  22. public NTStatus CreateFile(out object handle, out FileStatus fileStatus, string path, AccessMask desiredAccess, ShareAccess shareAccess, CreateDisposition createDisposition, CreateOptions createOptions, SecurityContext securityContext)
  23. {
  24. fileStatus = FileStatus.FILE_DOES_NOT_EXIST;
  25. // It is possible to have a named pipe that does not use RPC (e.g. MS-WSP),
  26. // However this is not currently needed by our implementation.
  27. RemoteService service = GetService(path);
  28. if (service != null)
  29. {
  30. // All instances of a named pipe share the same pipe name, but each instance has its own buffers and handles,
  31. // and provides a separate conduit for client/server communication.
  32. RPCPipeStream stream = new RPCPipeStream(service);
  33. handle = new FileHandle(path, false, stream, false);
  34. fileStatus = FileStatus.FILE_OPENED;
  35. return NTStatus.STATUS_SUCCESS;
  36. }
  37. handle = null;
  38. return NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
  39. }
  40. public NTStatus CloseFile(object handle)
  41. {
  42. FileHandle fileHandle = (FileHandle)handle;
  43. if (fileHandle.Stream != null)
  44. {
  45. fileHandle.Stream.Close();
  46. }
  47. return NTStatus.STATUS_SUCCESS;
  48. }
  49. private RemoteService GetService(string path)
  50. {
  51. if (path.StartsWith(@"\"))
  52. {
  53. path = path.Substring(1);
  54. }
  55. foreach (RemoteService service in m_services)
  56. {
  57. if (String.Equals(path, service.PipeName, StringComparison.InvariantCultureIgnoreCase))
  58. {
  59. return service;
  60. }
  61. }
  62. return null;
  63. }
  64. public NTStatus ReadFile(out byte[] data, object handle, long offset, int maxCount)
  65. {
  66. Stream stream = ((FileHandle)handle).Stream;
  67. data = new byte[maxCount];
  68. int bytesRead = stream.Read(data, 0, maxCount);
  69. if (bytesRead < maxCount)
  70. {
  71. // EOF, we must trim the response data array
  72. data = ByteReader.ReadBytes(data, 0, bytesRead);
  73. }
  74. return NTStatus.STATUS_SUCCESS;
  75. }
  76. public NTStatus WriteFile(out int numberOfBytesWritten, object handle, long offset, byte[] data)
  77. {
  78. Stream stream = ((FileHandle)handle).Stream;
  79. stream.Write(data, 0, data.Length);
  80. numberOfBytesWritten = data.Length;
  81. return NTStatus.STATUS_SUCCESS;
  82. }
  83. public NTStatus FlushFileBuffers(object handle)
  84. {
  85. FileHandle fileHandle = (FileHandle)handle;
  86. if (fileHandle.Stream != null)
  87. {
  88. fileHandle.Stream.Flush();
  89. }
  90. return NTStatus.STATUS_SUCCESS;
  91. }
  92. public NTStatus DeviceIOControl(object handle, uint ctlCode, byte[] input, out byte[] output, int maxOutputLength)
  93. {
  94. output = null;
  95. if (ctlCode == (uint)IoControlCode.FSCTL_PIPE_TRANSCEIVE)
  96. {
  97. int numberOfBytesWritten;
  98. NTStatus writeStatus = WriteFile(out numberOfBytesWritten, handle, 0, input);
  99. if (writeStatus != NTStatus.STATUS_SUCCESS)
  100. {
  101. return writeStatus;
  102. }
  103. NTStatus readStatus = ReadFile(out output, handle, 0, maxOutputLength);
  104. if (readStatus != NTStatus.STATUS_SUCCESS)
  105. {
  106. return readStatus;
  107. }
  108. return NTStatus.STATUS_SUCCESS;
  109. }
  110. return NTStatus.STATUS_NOT_SUPPORTED;
  111. }
  112. public NTStatus QueryDirectory(out List<QueryDirectoryFileInformation> result, object directoryHandle, string fileName, FileInformationClass informationClass)
  113. {
  114. result = null;
  115. return NTStatus.STATUS_NOT_SUPPORTED;
  116. }
  117. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  118. {
  119. switch (informationClass)
  120. {
  121. case FileInformationClass.FileBasicInformation:
  122. {
  123. FileBasicInformation information = new FileBasicInformation();
  124. information.FileAttributes = FileAttributes.Temporary;
  125. result = information;
  126. return NTStatus.STATUS_SUCCESS;
  127. }
  128. case FileInformationClass.FileStandardInformation:
  129. {
  130. FileStandardInformation information = new FileStandardInformation();
  131. information.DeletePending = false;
  132. result = information;
  133. return NTStatus.STATUS_SUCCESS;
  134. }
  135. case FileInformationClass.FileNetworkOpenInformation:
  136. {
  137. FileNetworkOpenInformation information = new FileNetworkOpenInformation();
  138. information.FileAttributes = FileAttributes.Temporary;
  139. result = information;
  140. return NTStatus.STATUS_SUCCESS;
  141. }
  142. default:
  143. result = null;
  144. return NTStatus.STATUS_INVALID_INFO_CLASS;
  145. }
  146. }
  147. public NTStatus SetFileInformation(object handle, FileInformation information)
  148. {
  149. return NTStatus.STATUS_NOT_SUPPORTED;
  150. }
  151. public NTStatus GetFileSystemInformation(out FileSystemInformation result, FileSystemInformationClass informationClass)
  152. {
  153. result = null;
  154. return NTStatus.STATUS_NOT_SUPPORTED;
  155. }
  156. }
  157. }